<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class store_db extends Model
{
	public $pk = 'store_id';
	public $table = 'ptx_store';
	var $linker = array(
			'user'=>array(
					'type' => 'hasone',
					'map' => 'user',
					'mapkey' => 'user_id',
					'ftable' => 'ptx_user',
					'fkey' => 'user_id',
					'enabled' => true
			),
			'category'=>array(
					'type' => 'hasone',
					'map' => 'category',
					'mapkey' => 'category_id',
					'ftable' => 'ptx_store_category',
					'fkey' => 'category_id',
					'enabled' => true
			),
	);
	var $select_fields = " ptx_store.*, category.*, user.nickname, user.email ";

	private function init_conditions($conditions){
		$conditions_store = NULL;
		if(isset($conditions['keyword'])){
			$keyword = $this->escape($conditions['keyword']);
			$conditions_store .= "AND MATCH (ptx_store.keywords_search) AGAINST ('{$keyword}' IN BOOLEAN MODE) ";
		}
		if(isset($conditions['likekeyword'])){
			$likeword = $this->escape("%{$conditions['likekeyword']}%");
			$conditions_store .= "AND ptx_store.store_name LIKE $likeword ";
		}
		if(isset($conditions['category_id'])){
			$category_id = $this->escape($conditions['category_id']);
			$conditions_store .= "AND ptx_store.category_id={$category_id} ";
		}
		if(isset($conditions['user_id'])){
			$user_id = $this->escape($conditions['user_id']);
			$conditions_store .= "AND ptx_store.user_id={$user_id} ";
		}
		if(isset($conditions['poster_id'])){
			$poster_id = $this->escape($conditions['poster_id']);
			$conditions_store .= "AND ptx_store.poster_id={$poster_id} ";
		}
		if(strpos($conditions_store, 'AND') === 0){
			$conditions_store = substr($conditions_store, 3);
		}
		return $conditions_store;
	}

	public function search($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_store.store_id DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}

	public function search_no_page($conditions=NULL,$fields = null,$sort=null,$limit){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_store.store_id DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->findAllJoin($conditions,$sort,$fields,$limit);
	}

	public function find_one($conditions=NULL){
		$fields = $this->select_fields;
		return $this->findJoin($conditions,null,$fields);
	}

	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			$result = $this->create($data);
			return $result;
		}
		return false;
	}

	private function check_value($data){
		if(!is_numeric($data['category_id'])){
			return false;
		}
		return true;
	}
	
	public function change_category($category_id,$cateid){
		return $this->update(array('category_id'=>$category_id), array('category_id'=>$cateid));
	}

	public function deletestore($storeid){
		if($storeid){
			load_api('share')->deleteShareByCondition(array('store_id'=>$storeid));
			$this->deleteByPk($storeid);
		}
	}

	public function updateStoreByid($storeid,$data){
		return $this->update(array('store_id'=>$storeid), $data);
	}

	public function getStoreByid($storeid){
		return $this->find_one(array('store_id'=>$storeid));
	}

	public function add_like($storeid){
		return $this->incrField(array('store_id'=>$storeid), 'total_like');
	}
	
	public function remove_like($storeid){
		return $this->decrField(array('store_id'=>$storeid), 'total_like');
	}

	public function add_visit($storeid){
		return $this->incrField(array('total_visit'=>$storeid), 'total_like');
	}
	
	public function remove_visit($storeid){
		return $this->decrField(array('total_visit'=>$storeid), 'total_like');
	}
}
